<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/'+goods.categories[0].id">{{goods.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub/'+goods.categories[1].id">{{goods.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem >{{goods.name}}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左边图片 -->
        <div class="media">
          <goods-image :images="goods.mainPictures"></goods-image>
          <goods-sales></goods-sales>
        </div>
        <!-- 右边描述 -->
        <div class="spec">
          <goods-name :goods="goods"></goods-name>
          <!-- sku -->
          <goods-sku :goods="goods" @change="changeSku" :skuId="goods.skus[0].id"></goods-sku>

          <!-- 数量组件 -->
          <xtx-numbox label="数量" v-model="num" :max="goods.inventory"></xtx-numbox>

          <!-- 加入购物车 -->
          <xtx-button type="primary" style="margin-top:20px;" @click="insertCart">加入购物车</xtx-button>
        </div>
      </div>

      <!-- 商品推荐 -->
      <goods-relevant :goodsId="goods.id"></goods-relevant>

      <!-- 商品详情 -->
      <div class="goods-footer">
        <!-- 详情页 -->
        <div class="goods-article">
          <!-- 商品+评价 -->

            <goods-tabs></goods-tabs>

          <!-- 注意事项 -->
          <div class="goods-warn">
            <goods-warn></goods-warn>
          </div>
        </div>

        <!-- 热榜 -->
        <div class="goods-aside">
          <goods-hot :type="1" :goodsId="goods.id"></goods-hot>
          <goods-hot :type="2" :goodsId="goods.id"></goods-hot>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import GoodsRelevant from '@/views/goods/components/goods-relevant'
import GoodsImage from '@/views/goods/components/goods-image'
import GoodsSales from '@/views/goods/components/goods-sales'
import GoodsName from '@/views/goods/components/goods-name'
import GoodsSku from '@/views/goods/components/goods-sku'
import GoodsTabs from '@/views/goods/components/goods-tabs'
import GoodsHot from '@/views/goods/components/goods-hot'
import GoodsWarn from '@/views/goods/components/goods-warn'
import { findGoods } from '@/api/product.js'
import { ref, watch, nextTick, provide } from 'vue'
import { useRoute } from 'vue-router'
import message from '@/components/library/message'
import { useStore } from 'vuex'
export default {
  name: 'GoodsPage',

  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn

  },

  setup () {
    const goods = useGoods()

    const num = ref(1)
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }

    // 为后代提供goods数据
    provide('goods', goods)

    // 加入购物车的逻辑
    const currSku = ref(null)
    const store = useStore()
    const insertCart = () => {
      if (!currSku.value) {
        return message({ text: '请选择商品规格' })
      }
      if (num.value > goods.inventory) {
        return message({ text: '库存不足' })
      }
      store.dispatch('cart/insertCart', {
        id: goods.value.id,
        skuId: currSku.value.skuId,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        price: currSku.value.price,
        nowPrice: currSku.value.price,
        count: num.value,
        attrsText: currSku.value.specsText,
        selected: true,
        isEffective: true,
        stock: currSku.value.inventory
      }).then(() => {
        message({ text: '加入购物车成功', type: 'success' })
      })
    }
    return { goods, changeSku, num, insertCart }
  }

}

const useGoods = () => {
  const goods = ref(null)
  const route = useRoute()

  watch(() => route.params.id, (newVal) => {
    if (newVal && route.path === `/product/${newVal}`) {
      findGoods(route.params.id).then(data => {
        // 让商品数据为null让后使用v-if的组件可以重新销毁和创建
        goods.value = null
        nextTick(() => {
          goods.value = data.result
        })
      })
    }
  }, { immediate: true })

  return goods
}
</script>

<style lang="less" scoped>
.goods-info {
  min-height: 600px;
  background: #fff;
   display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
//.goods-tabs {
 // min-height: 600px;
 // background: #fff;
//}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
